﻿using Google.GData.Client;
using Google.GData.Spreadsheets;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Copycat
{
    public partial class BroadCastingMessage : System.Web.UI.Page
    {
        string subscribedMembers;
        string discountMembers;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                subscribedMembers = (string)Session["subscribed"];
                discountMembers = (string)Session["discount"];
                return;
            }

            AtomEntryCollection rows = DatabaseManager.RetrieveRows("emails");

            subscribedMembers = "";
            foreach (ListEntry ae in rows)
                subscribedMembers += ae.Elements[1].Value + " , ";
            subscribedMembers = subscribedMembers.Remove(subscribedMembers.Length - 1);


            rows = DatabaseManager.RetrieveRows("discountids");

            discountMembers = "";
            foreach (ListEntry ae in rows)
                discountMembers += ae.Elements[2].Value + " , ";
            discountMembers = discountMembers.Remove(discountMembers.Length - 1);

            Session["discount"] = discountMembers;
            Session["subscribed"] = subscribedMembers;
        }

        protected void listTo_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listTo.SelectedValue == "0")
                txtEmails.Text = subscribedMembers;
            else
                txtEmails.Text = discountMembers;
        }

        protected void btnSend_Click(object sender, EventArgs e)
        {
            MailSender.SendMail(txtEmails.Text, txtSubject.Text, txtMessage.Text);
        }

       
    }
}